import Vue from 'vue'
import Vuex from 'vuex'
import tableStore from './modules/tableStore'
import axios from '../http/http'
import * as api from '../http/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user:{
      // 当项目加载  获取sessionStorage里面的信息  如果没有信息  初始化为undefined
      userName:sessionStorage.getItem('userName') || undefined,
      type:sessionStorage.getItem('type') || undefined,
      token:sessionStorage.getItem('token') || undefined
    }
  },
  mutations: {
    addUser(state,obj){
      state.user.userName = obj.data.user
      state.user.type= obj.data.type
      state.user.token= obj.token
    },
    loginOut(state){
      state.user = {}
    }
  },
  actions: {
    login({commit},obj){
      return axios({
          url:api.login,
          data:obj,
          method:'post'
        }).then(res=>{
          sessionStorage.setItem('userName',res.data.data.user)
          sessionStorage.setItem('type',res.data.data.type)
          commit('addUser',res.data)
          return res.data.code
        })
    },
    loginOut({commit}){
      sessionStorage.removeItem('userName')
      sessionStorage.removeItem('token')
      sessionStorage.removeItem('type')
      commit('loginOut')
    }
  },
  getters:{
    getType(state){
      if(state.user.type === 'admin'){
        return '管理员'
      }else if(state.user.type==='user'){
        return '员工'
      }
    }, //getters就是vuex里面的计算属性
    quanxianNav(state) {
      if(state.user.type==='admin'){
        return true
      }else {
        return  false
      }
    }
  },//
  modules: {
    tableStore
  }
})
